﻿using System;
using System.Collections.Generic;
using System.Text;

namespace cici
{
    class Mvccontroller : Controller
    {

        public Dictionary<object, object> forColumns { get; set; }

        public Mvccontroller()
        {

        }

        public Boolean createMvccontroller(string className)
        {
            return this.createController(className);
        }

        private string formRequire(string className)
        {
            string resultTemplate=null;

            foreach (KeyValuePair<object, object> pair in this.forColumns)
            {
                resultTemplate += "\t\t\t\t$this->form_validation->set_rules('" + pair.Key +
                    "', '" + pair.Key + "', 'required');\n";
            }
            return resultTemplate;
        }

        private string formInputPost(string className)
        {
            string resultTemplate = "array(";

            foreach (KeyValuePair<object, object> pair in this.forColumns)
            {
                resultTemplate += "'" + pair.Key + "'=>$this->input->post('" + pair.Key + "'),";
            }
            resultTemplate += ");";
            resultTemplate = resultTemplate.Replace(",);", ");");

            return resultTemplate;
        }

        protected override string templateController(string file_location, string className)
        {
            string tempClassName = className.Substring(0, 1);
            string realClassName = tempClassName.ToUpper() + className.Remove(0, 1);

            string template =
                "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n" +
                "\n\n" +
                "class " + realClassName + " extends CI_Controller {\n" +
                "\n\n" +
                "\t/**\n" +
                "\t * Index Page for this controller.\n" +
                "\t *\n" +
                "\t * Maps to the following URL\n" +
                "\t * \t\thttp://example.com/index.php/" + className + " \n" +
                "\t *\t- or -  \n" +
                "\t * \t\thttp://example.com/index.php/" + className + " /index\n" +
                "\t *\t- or -\n" +
                "\t * Since this controller is set as the default controller in \n" +
                "\t * config/routes.php, it's displayed at http://example.com/\n" +
                "\t *\n" +
                "\t * So any other public methods not prefixed with an underscore will\n" +
                "\t * map to /index.php/welcome/<method_name>\n" +
                "\t * @see http://codeigniter.com/user_guide/general/urls.html\n" +
                "\t */\n" +
                "\t\n" +
                "\n\n" +
                "\tpublic function __construct()\n" +
                "\t{\n" +
                "\t\tparent::__construct();\n" +
                "\t\t\n" +
                "\t\t$this->load->model('tables/m_" + className + "');\n" +
                "\t}\n" +
                "\n\n" +
                "\tpublic function index()\n" +
                "\t{\n" +
                "\t\t$data=array('data'=>$this->m_" + className + "->getAll());\n" +
                "\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t$this->load->view('" + className + "/list',$data);\n" +
                "\t\t$this->load->view('headerfooter/footer');\n" +
                "\t}\n" +
                "\t\n" +
                "\tpublic function detail($id)\n" +
                "\t{\n" +
                "\t\tif(isset($id)){\n" +
                "\t\t\t$temp=$this->m_" + className + "->getById($id);\n" +
                "\t\t\tif($temp){\n" +
                "\t\t\t\t$data=array('data'=>$temp);\n" +
                "\t\t\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t\t\t$this->load->view('" + className + "/detail',$data);\n" +
                "\t\t\t\t$this->load->view('headerfooter/footer');\n" +
                "\t\t\t}else{\n" +
                "\t\t\t\tredirect(base_url('/" + className + "/index'));\n" +
                "\t\t\t}\n" +
                "\t\t}else{\n" +
                "\t\t\tredirect(base_url('/" + className + "/index'));\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\t\n" +
                "\tpublic function create()\n" +
                "\t{\n" +
                "\t\t//your code in here :)\n" +
                "\t\t" + this.formRequire(className) + "\n\n" +
                "\t\t\n" +
                "\t\tif ($this->form_validation->run()){\n" +
                "\t\t\t\n" +
                "\t\t\t$inputPost=" + this.formInputPost(className) + "\n\n" +
                "\t\t\t\n" +
                "\t\t\t$pesan=($this->m_" + className + "->insert($inputPost) ? 'Create Success' : 'Create Failed');\n" +
                "\t\t\t$data=array('data'=>array('pesan'=>$pesan));\n" +
                "\t\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t\t$this->load->view('" + className + "/create',$data);\n" +
                "\t\t\t$this->load->view('headerfooter/footer');\n" +
                "\t\t}else{\n" +
                "\t\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t\t$this->load->view('" + className + "/create');\n" +
                "\t\t\t$this->load->view('headerfooter/footer');\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\n\n" +
                "\tpublic function update($id)\n" +
                "\t{\n" +
                "\t\t//your code in here :)\n" +
                "\t\tif(isset($id)){\n" +
                "\t\t\t$temp=$this->m_" + className + "->getById($id);\n" +
                "\t\t\tif($temp){\n" +
                "\t\t\t\t\n" +
                "\t\t\t\t$this->form_validation->set_rules('id" + className + "', 'required');\n" +
                "\t\t\t\t\n" +
                "\t\t\t\t" + this.formRequire(className) + "\n\n" +
                "\t\t\t\t\n" +
                "\t\t\t\tif ($this->form_validation->run()){\n" +
                "\t\t\t\t\n" +
                "\t\t\t\t\t$inputPost=" + this.formInputPost(className) + "\n\n" +
                "\t\t\t\t\t\n" +
                "\t\t\t\t\t$pesan=($this->m_" + className + "->update($this->input->post('id" + className + "'),$inputPost) ? 'Update Success' : 'Update Failed');\n" +
                "\t\t\t\t\t$temp['pesan']=$pesan;\n" +
                "\t\t\t\t\t$data=array('data'=>$temp);\n" +
                "\t\t\t\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t\t\t\t$this->load->view('" + className + "/update',$data);\n" +
                "\t\t\t\t\t$this->load->view('headerfooter/footer');\n" +
                "\t\t\t\t\n" +
                "\t\t\t\t}else{\n" +
                "\t\t\t\t\n" +
                "\t\t\t\t\t$data=array('data'=>$temp);\n" +
                "\t\t\t\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t\t\t\t$this->load->view('" + className + "/update',$data);\n" +
                "\t\t\t\t\t$this->load->view('headerfooter/footer');\n" +
                "\t\t\t\t\t\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}else{\n" +
                "\t\t\t\tredirect(base_url('/" + className + "/index'));\n" +
                "\t\t\t}\n" +
                "\t\t}else{\n" +
                "\t\t\tredirect(base_url('/" + className + "/index'));\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\n\n" +
                "\tpublic function delete($id)\n" +
                "\t{\n" +
                "\t\t//your code in here :)\n" +
                "\t\tif(isset($id)){\n" +
                "\t\t\t$pesan=($this->m_" + className + "->delete($id) ? 'Delete Success' : 'Delete Failed');\n" +
                "\t\t\t$data=array('data'=>array('pesan'=>$pesan));\n" +
                "\t\t\t$this->load->view('headerfooter/header');\n" +
                "\t\t\t$this->load->view('" + className + "/delete',$data);\n" +
                "\t\t\t$this->load->view('headerfooter/footer');\n" +
                "\t\t}else{\n" +
                "\t\t\tredirect(base_url('/" + className + "/index'));\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}\n" +
                "\n\n" +
                "/* End of file " + className + ".php */\n" +
                "/* Location: " + file_location + " */\n" +
                "/* Generated by CICI */";

            return template;
        }
    }
}
